<?php
namespace app\shop\service\company;
use app\common\service\Tree;
use app\shop\model\company\Department as DepartmentModel;
use app\shop\model\company\Staff;
use app\common\service\Result;
class Department
{
    public function lists($data)
    {
        $map = [
            ['app_id', '=', $data['app_id']],
        ];
        $lists = DepartmentModel::where($map)->with([
            'managerUser' => function ($query) {
                return $query->field('user_id,nickName,mobile,real_name');
            }
        ])->order('pid,sort desc')->select()->toArray();
        $lists = Tree::setConfig('id', 'pid', 'children')->makeTree($lists);
        return Result::success($lists);
    }

    public function options($app_id)
    {
        $map = [
            ['app_id', '=', $app_id],
        ];
        $lists = DepartmentModel::where($map)->field('id,pid,title')->order('pid,sort desc')->select()->toArray();
        $lists = Tree::setConfig('id', 'pid', 'children')->makeTree($lists);
        return Result::success($lists);
    }

    public function getChildIds($id)
    {
        $map = [
            ['pid', '=', $id],
        ];
        $ids = DepartmentModel::where($map)->column('id');
        return $ids;
    }
}